//
//  SMTabBarController.m
//  DiDi
//
//  Created by huangtie on 2017/3/1.
//  Copyright © 2017年 Sinmore. All rights reserved.
//

#import "SMTabBarController.h"
#import "ZBHomeViewController.h"
#import "ZBMeViewController.h"
#import "ZBLivePlanViewController.h"
#import "SMNavigationController.h"


@interface SMTabBarController ()

@property (nonatomic , weak) UIView *roundView;

@end

@implementation SMTabBarController

//- (BOOL)shouldAutorotate
//{
//    return NO;
//}
//
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskPortrait;
//}
//
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
//    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
//}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.roundView.centerX = SCREEN_WIDTH / 2;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    
    self.tabBar.backgroundColor = UICOLOR_ARGB(0xffffffff);
    self.view.clipsToBounds = YES;
    
    UIView *roundView = [[UIView alloc] init];
    roundView.size = CGSizeMake(72, 72);
    roundView.backgroundColor = [UIColor clearColor];
    roundView.clipsToBounds = YES;
    roundView.layer.cornerRadius = roundView.width / 2;
    roundView.layer.borderWidth = 0;
    roundView.centerX = SCREEN_WIDTH / 2;
    roundView.centerY = 10;
    [self.tabBar addSubview:roundView];
    self.roundView = roundView;
    
    UIButton *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerButton setBackgroundImage:[UIImage imageNamed:@"home_cctv"] forState:UIControlStateNormal];
    centerButton.size = roundView.size;
    centerButton.left = 1;
    centerButton.top = 1;
    [centerButton addTarget:self action:@selector(roundClick) forControlEvents:UIControlEventTouchUpInside];
    [roundView addSubview:centerButton];

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:UICOLOR_ARGB(0xff999999),NSFontAttributeName : FONT_NORMAL_12} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:SMCOLOR_RED} forState:UIControlStateSelected];

    ZBHomeViewController *homeVC = [[ZBHomeViewController alloc] init];
    SMNavigationController *homeNav = [[SMNavigationController alloc] initWithRootViewController:homeVC];
    homeNav.tabBarItem.title = @"首页";
    homeNav.tabBarItem.image = [[UIImage imageNamed:@"home_vidio"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"home_vidio_s"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    ZBMeViewController *meVC = [[ZBMeViewController alloc] init];
    SMNavigationController *meNav = [[SMNavigationController alloc] initWithRootViewController:meVC];
    meNav.tabBarItem.title = @"我的";
    meNav.tabBarItem.image = [[UIImage imageNamed:@"home_user"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    meNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"home_user_s"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    SMViewController *spacingVC = [[SMViewController alloc] init];
    SMNavigationController *spacingNav = [[SMNavigationController alloc] initWithRootViewController:spacingVC];
    spacingNav.tabBarItem.enabled = NO;
    self.viewControllers = @[homeNav,spacingNav,meNav];
}

- (void)roundClick
{
    BOOL isLogin = [[SMUserManager shareManager] isLogin];
    [[SMUserManager shareManager] cheakLogin:self callBack:^(NSString *token) {
        CGFloat dl = 0;
        if (isLogin) {
            dl = .3;
        }
        [self performBlock:^{
            ZBLivePlanViewController *livePlan = [[ZBLivePlanViewController alloc] init];
            [self presentViewController:livePlan animated:YES completion:nil];
        } afterDelay:dl];
    } cancelBlock:nil];
}

- (void)jumpPage:(SMTabBar_Code)code
{
    if (code < self.viewControllers.count) {
        self.selectedIndex = code;
    }
}

- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay
{
    block = [block copy];
    
    [self performSelector:@selector(fireBlockAfterDelay:) withObject:block afterDelay:delay];
}

- (void)fireBlockAfterDelay:(void (^)(void))block
{
    block();
}

@end
